import 'dart:typed_data';
import 'package:binary/binary.dart';
import 'package:inmotion_protocol/common/product_model.dart';
import 'package:inmotion_protocol/common/product_version.dart';
import 'package:inmotion_protocol/lorin/bean/common/date.dart';
import 'package:inmotion_protocol/lorin/util.dart';
import 'package:inmotion_protocol/util/byte_util.dart';

class VehicleProductInfo {
  Uint16 format = Uint16.zero;
  Uint8 interfaceVersion = Uint8.zero;
  String sn = '';
  VehicleProductFeature productFeature = VehicleProductFeature();
  VersionWrapper versionWrapper = VersionWrapper();
  Datetime activationDate = Datetime();

  String hexCode = '';

  VehicleProductInfo(
      this.format,
      this.interfaceVersion,
      this.sn,
      this.productFeature,
      versionWrapper,
      this.activationDate,
      );

  VehicleProductInfo.fromBytes(List<int> bytes) {
    if (bytes.length < 4) return;
    Uint8ClampedList byteList = Uint8ClampedList.fromList(bytes);
    format = ByteUtil.bytes2Uint16Le(byteList, 0);
    interfaceVersion = ByteUtil.bytes2Uint8Le(byteList, 2);

    int snLength = 21;
    if (snLength > 0 && bytes.length >= 3 + snLength) {
      sn = String.fromCharCodes(byteList.sublist(3, 3 + snLength));
    }

    productFeature = VehicleProductFeature.fromBytes(
        byteList.sublist(3 + snLength, 7 + snLength));

    if(productFeature.mainSeries.value == ProductModel.euc && productFeature.subSeries.value == ProductModel.V9){
      versionWrapper = Util.parseFwVersion(byteList.sublist(7 + snLength, 61 + snLength));

      Datetime.fromBytes(byteList.sublist(61 + snLength, 65 + snLength), output: activationDate);

      versionWrapper = Util.parseHwVersion(
          byteList.sublist(65 + snLength),
          output: versionWrapper);
    }
    if(productFeature.mainSeries.value == ProductModel.euc && (productFeature.subSeries.value == ProductModel.V12S || productFeature.subSeries.value == ProductModel.P6)){
      versionWrapper = Util.parseFwVersion(byteList.sublist(7 + snLength, 70 + snLength));

      Datetime.fromBytes(byteList.sublist(70 + snLength, 74 + snLength), output: activationDate);

      versionWrapper = Util.parseHwVersion(
          byteList.sublist(74 + snLength),
          output: versionWrapper);
    }

  }
}

class VehicleProductFeature {
  Uint8 mainSeries = Uint8.zero; // 主系列
  Uint8 subSeries = Uint8.zero; // 子系列
  Uint8 model = Uint8.zero; // 具体型号
  Uint8 batch = Uint8.zero; // 批次特征

  String hexCode = '';

  VehicleProductFeature();

  VehicleProductFeature.fromBytes(List<int> bytes) {
    Uint8ClampedList byteList = Uint8ClampedList.fromList(bytes);
    if (bytes.length < 4) return;
    mainSeries = ByteUtil.bytes2Uint8Le(byteList, 0);
    subSeries = ByteUtil.bytes2Uint8Le(byteList, 1);
    model = ByteUtil.bytes2Uint8Le(byteList, 2);
    batch = ByteUtil.bytes2Uint8Le(byteList, 3);
  }

}


class VehicleFirmwareVersion {
  Uint8 hmicDeviceId = Uint8.zero; // 人机板设备号
  Uint32 hmicAppVersion = Uint32.zero; // 人机板应用程序版本号
  Uint32 hmicBootVersion = Uint32.zero; // 人机板引导程序版本号
  Uint8 mcDeviceId = Uint8.zero; // 驱动板设备号
  Uint32 mcAppVersion = Uint32.zero; // 驱动板应用程序版本号
  Uint32 mcBootVersion = Uint32.zero; // 驱动板引导程序版本号
  Uint8 bleDeviceId = Uint8.zero; // 蓝牙板设备号
  Uint32 bleAppVersion = Uint32.zero; // 蓝牙板应用程序版本号
  Uint32 bleBootVersion = Uint32.zero; // 蓝牙板引导程序版本号
  Uint8 bms1DeviceId = Uint8.zero; // 电池1设备号
  Uint32 bms1AppVersion = Uint32.zero; // 电池1应用程序版本号
  Uint32 bms1BootVersion = Uint32.zero; // 电池1引导程序版本号
  Uint8 bms2DeviceId = Uint8.zero; // 电池2设备号
  Uint32 bms2AppVersion = Uint32.zero; // 电池2应用程序版本号
  Uint32 bms2BootVersion = Uint32.zero; // 电池2引导程序版本号
  Uint8 dcDeviceId = Uint8.zero; // 显示屏设备号
  Uint32 dcAppVersion = Uint32.zero; // 显示屏应用程序版本号
  Uint32 dcBootVersion = Uint32.zero; // 显示屏引导程序版本号
  Uint8 tboxDeviceId = Uint8.zero; // 云盒设备号
  Uint32 tboxAppVersion = Uint32.zero; // 云盒应用程序版本号
  Uint32 tboxBootVersion = Uint32.zero; // 云盒引导程序版本号

  VehicleFirmwareVersion();

  VehicleFirmwareVersion.fromBytes(List<int> bytes) {
    if (bytes.length < 63) return;
    var byteList = Uint8ClampedList.fromList(bytes);
    hmicDeviceId = ByteUtil.bytes2Uint8Le(byteList, 0);
    hmicAppVersion = ByteUtil.bytes2Uint32Le(byteList, 1);
    hmicBootVersion = ByteUtil.bytes2Uint32Le(byteList, 5);
    mcDeviceId = ByteUtil.bytes2Uint8Le(byteList, 9);
    mcAppVersion = ByteUtil.bytes2Uint32Le(byteList, 10);
    mcBootVersion = ByteUtil.bytes2Uint32Le(byteList, 14);
    bleDeviceId = ByteUtil.bytes2Uint8Le(byteList, 18);
    bleAppVersion = ByteUtil.bytes2Uint32Le(byteList, 19);
    bleBootVersion = ByteUtil.bytes2Uint32Le(byteList, 23);
    bms1DeviceId = ByteUtil.bytes2Uint8Le(byteList, 27);
    bms1AppVersion = ByteUtil.bytes2Uint32Le(byteList, 28);
    bms1BootVersion = ByteUtil.bytes2Uint32Le(byteList, 32);
    bms2DeviceId = ByteUtil.bytes2Uint8Le(byteList, 36);
    bms2AppVersion = ByteUtil.bytes2Uint32Le(byteList, 37);
    bms2BootVersion = ByteUtil.bytes2Uint32Le(byteList, 41);
    dcDeviceId = ByteUtil.bytes2Uint8Le(byteList, 45);
    dcAppVersion = ByteUtil.bytes2Uint32Le(byteList, 46);
    dcBootVersion = ByteUtil.bytes2Uint32Le(byteList, 50);
    tboxDeviceId = ByteUtil.bytes2Uint8Le(byteList, 54);
    tboxAppVersion = ByteUtil.bytes2Uint32Le(byteList, 55);
    tboxBootVersion = ByteUtil.bytes2Uint32Le(byteList, 59);
  }
}

class VehicleHardwareVersion {
  Uint16 hmicHwVersion = Uint16.zero; // 人机板硬件版本
  Uint16 mcHwVersion = Uint16.zero; // 驱动板硬件版本
  Uint16 bleHwVersion = Uint16.zero; // 蓝牙板硬件版本

  VehicleHardwareVersion();

  VehicleHardwareVersion.fromBytes(List<int> bytes) {
    if (bytes.length < 6) return;
    var byteList = Uint8ClampedList.fromList(bytes);
    hmicHwVersion = ByteUtil.bytes2Uint16Le(byteList, 0);
    mcHwVersion = ByteUtil.bytes2Uint16Le(byteList, 2);
    bleHwVersion = ByteUtil.bytes2Uint16Le(byteList, 4);
  }

}
